import Vue from 'vue'
import VueRouter from 'vue-router'
const Home = () => import('@/views/Home/index')
const Search = () => import('@/views/Search/index')
const Login = () => import('@/views/Login/index')
const Register = () => import('@/views/Register/index')
const Detail = () => import('@/views/Detail/index')
const AddCartSuccess = () => import('@/views/AddCartSuccess/index')
const ShopCart = () => import('@/views/ShopCart/index')
const Trade = () => import('@/views/Trade/index')
const Pay = () => import('@/views/Pay/index')
const PaySuccess = () => import('@/views/PaySuccess/index')

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    alias: '/',
    name: 'home',
    component: Home,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/search/:keyword?',
    name: 'search',
    component: Search,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/detail/:skuid?',
    name: 'detail',
    component: Detail,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/addcartsuccess',
    name: 'addcartsuccess',
    component: AddCartSuccess,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/trade',
    name: 'trade',
    component: Trade,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/pay',
    name: 'pay',
    component: Pay,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/paysuccess',
    name: 'paysuccess',
    component: PaySuccess,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/shopcart',
    name: 'shopcart',
    component: ShopCart,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  }
]

const router = new VueRouter({
  routes,
  // 路由滚动行为
  scrollBehavior (to, from, savedPosition) {
    // return { x: 0, y: 0 }
    return { y: 0 }
  }
})

// 保存路由原始的跳转方法 
const originalPush = VueRouter.prototype.push
// 重写路由的原型中的push方法
VueRouter.prototype.push = function push(location) {
  // 捕获错误 push方法返回的是一个promise对象
  return originalPush.call(this, location).catch(err => err)
}


export default router
